<?php
namespace Org\Wx;
/**
 * 微信操作类
 */
class wx{
	/**
	 * 生成自定义菜单
	 */
	public static function createmenu($app_id,$app_secret){
		$list = M('wx_menu')->where("parent_id='0'")->select();
		if(empty($list)){
			return "你还没有生成任何的菜单";
		}else{
			$res = array();
			foreach ($list as $i=>$va){
				$res[$i]['name'] = $va['name'];
				switch ($va['action']){
					case '1':
						$res[$i]['type'] = "click";
						$res[$i]['key'] = "keyword";
					break;
					case '2':
						$res[$i]['type'] = "click";
						$res[$i]['key'] = "zhannei";
					break;
					case '3':
						$res[$i]['type'] = 'view';
						$res[$i]['url'] = $va['url'];
					break;
				}
				$sub_button[$i] =  M('wx_menu')->where("parent_id='{$va['id']}'")->select();
				if(!empty($sub_button[$i])){
					foreach ($sub_button[$i] as $k=>$vv){
						$res[$i]['sub_button'][$k]['name'] = $vv['name'];
						switch ($vv['action']){
							case '1':
								$res[$i]['sub_button'][$k]['type'] = "click";
								$res[$i]['sub_button'][$k]['key'] = "keyword";
							break;
							case '2':
								$res[$i]['sub_button'][$k]['type'] = "click";
								$res[$i]['sub_button'][$k]['key'] = "zhannei";
							break;
							case '3':
								$res[$i]['sub_button'][$k]['type'] = 'view';
								$res[$i]['sub_button'][$k]['url'] = $vv['url'];
							break;
						}
					}
				}
				
			}
			$newmeu['button'] = $res;
			$button =  jsonencode($newmeu);
			$token = checkToken(0,$app_id,$app_secret);
			$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$token;
			$msg = http_request($url,$button);
			return json_decode($msg,true);
		}
	}
	/**
	 * 上传图片、语音、视频到微信素材里
	 * $type  1是图片 2语音3是视频
	 */
	public static function add_img($type,$file,$app_id,$app_secret){
		switch ($type){
			case '1':
				$typename = 'image';
				$img_info = getimagesize('.'.$file);
				$file_info['content-type'] = $img_info[3];
			break;
			case '2':
				$typename = 'voice';
			break;
			case '3':
				$typename = 'video';
			break;
			case '4':
				$typename = 'thumb';
			break;
			default:
				return false;exit();
			break;
		}
		$file_info['filename'] = $file;
		$file_info['filelength'] = ceil(filesize('.'.$file));
		//$access_token=get_access_token();
		$token = checkToken(0,$app_id,$app_secret);
		$url="http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token={$token}&type={$typename}";
		$ch1 = curl_init ();
		$timeout = 5;
		$real_path="{$_SERVER['DOCUMENT_ROOT']}{$file_info['filename']}";
		$data= array("media"=>"@{$real_path}");
		curl_setopt ( $ch1, CURLOPT_SAFE_UPLOAD, false);
		curl_setopt ( $ch1, CURLOPT_URL, $url );
		curl_setopt ( $ch1, CURLOPT_POST, 1 );
		curl_setopt ( $ch1, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt ( $ch1, CURLOPT_CONNECTTIMEOUT, $timeout );
		curl_setopt ( $ch1, CURLOPT_SSL_VERIFYPEER, FALSE );
		curl_setopt ( $ch1, CURLOPT_SSL_VERIFYHOST, false );
		curl_setopt ( $ch1, CURLOPT_POSTFIELDS, $data );
		$result = curl_exec ( $ch1 );
		curl_close ( $ch1 );
		if(curl_errno()==0){
			$result=json_decode($result,true);
			return $result;
		}else{
			return false;
		}
	}
	/**
	 * 发送图文消息时--上传图片
	 */
	public static function add_newsimg($img,$app_id,$app_secret){
		$img_info = getimagesize('.'.$img);
		$file_info['filename'] = $img;
		$file_info['content-type'] = $img_info[3];
		$file_info['filelength'] = ceil(filesize('.'.$img));
		//$access_token=get_access_token();
		$token = checkToken(0,$app_id,$app_secret);
		$url="https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={$token}&type=image";
		$ch1 = curl_init ();
		curl_setopt ( $ch1, CURLOPT_SAFE_UPLOAD, false);
		$timeout = 5;
		$real_path="{$_SERVER['DOCUMENT_ROOT']}{$file_info['filename']}";
		//$real_path=str_replace("/", "\\", $real_path);
		$data= array("media"=>"@{$real_path}",'form-data'=>$file_info);
		curl_setopt ( $ch1, CURLOPT_URL, $url );
		curl_setopt ( $ch1, CURLOPT_POST, 1 );
		curl_setopt ( $ch1, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt ( $ch1, CURLOPT_CONNECTTIMEOUT, $timeout );
		curl_setopt ( $ch1, CURLOPT_SSL_VERIFYPEER, FALSE );
		curl_setopt ( $ch1, CURLOPT_SSL_VERIFYHOST, false );
		curl_setopt ( $ch1, CURLOPT_POSTFIELDS, $data );
		$result = curl_exec ( $ch1 );
		curl_close ( $ch1 );
		if(curl_errno()==0){
			$result=json_decode($result,true);
			//print_r($result);exit();
			return $result;exit();
			return $result['media_id'];
		}else {
			return false;
		}
	}
	/**
	 * 上传图文素材到微信里
	 */ 
	public static function uploadnews($newstextlist,$app_id,$app_secret){
		$json['articles'] = $newstextlist;
		$newjson = jsonencode($json);
		$token = checkToken(0,$app_id,$app_secret);
		$url = "https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=".$token;
		$msg = http_request($url,$newjson);
		$result = json_decode($msg,true);
		return $result;
	}
	/**
	 * 发送群发消息--上传视频获取media_id
	 */
	public static function uploadvideo($media_id,$title=null,$description=null,$app_id,$app_secret){
		$json['media_id'] = $media_id;
		$json['title'] = $title;
		$json['description'] = $description;
		$newjson = jsonencode($json);
		$token = checkToken(0,$app_id,$app_secret);
		$url = "https://file.api.weixin.qq.com/cgi-bin/media/uploadvideo?access_token=".$token;
		$msg = http_request($url,$newjson);
		$result = json_decode($msg,true);
		return $result;
	} 
	/**
	 * 发送群发消息（根据oppenid）来发
	 */
	public function send_nenmsg($type,$userid,$wx_id,$app_id,$app_secret){
		switch ($type){
			case '1':
				//文本
			  $json['msgtype'] = "text";
			break;
			case '2':
				//图文
			   $json['msgtype'] = "mpnews";
			break;
			case '3':
				//语音
				$json['msgtype'] = "voice";
			break;
			case '4':
				//视频
				$json['msgtype'] = "mpvideo";
			break;
			case '5':
				//图片
				$json['msgtype'] = "image";
			break;
		}
		$token = checkToken(0,$app_id,$app_secret);
		$json['touser'] = $userid;
		if($type == 4){
			$json[$json['msgtype']]['media_id'] = $wx_id['wx_id'];
			$json[$json['msgtype']]['title'] = $wx_id['name'];
			$json[$json['msgtype']]['description'] = $wx_id['mark'];
		}elseif($type == 1){
			$json[$json['msgtype']]['content'] = $wx_id;
		}else{
			$json[$json['msgtype']]['media_id'] = $wx_id;
		}
		$newjson = jsonencode($json);
		$url = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=".$token;
		$msg = http_request($url,$newjson);
		$result = json_decode($msg,true);
		return $result;
	}
}